﻿using UnityEngine;
using System.Collections;

namespace DragonRescue3D
{
    /// <summary>
    /// Enumerated type defining the state of the Rocket
    /// </summary>
    public enum RocketState
    {
        Dormant,
        Positioning,
        Firing
    }

    /// <summary>
    /// Contains all actions and logic associated with the rocket itself
    /// </summary>
    public class Rocket : MonoBehaviour
    {
        public Transform SliderOrigin;
        public Transform SliderEndPoint;
        public float _speed = 0.1f;
        public float _lifeTime = 2.0f;
        RocketState State = RocketState.Dormant;
        public AudioClip Fuse;
        public AudioClip TakeOff;
        GamePlayManager _gamePlayManager;

       /// <summary>
       /// Initialise the state of the rocket on start-up
       /// </summary>
        void Start()
        {
            State = RocketState.Dormant;
            _gamePlayManager = GameObject.FindObjectOfType(typeof(GamePlayManager)) as GamePlayManager;
        }

        /// <summary>
        /// Checks the state of the rocket every frame and takes appropriate action
        /// </summary>
        void Update()
        {
            // If we're being positioned then clamp the rocket to the slider 
            if (IsActive)
            {
                ClampRocketToSlider();
            }
            else if (HasFired) //If we've been fired then update our position
            {
                UpdateVerticalPosition();
            }
            else if (IsInactive && gameObject.activeInHierarchy) // else activate ourselves
            {
                gameObject.SetActive(false);
            }
        }

        /// <summary>
        /// Update the game status once destroyed
        /// </summary>
        void OnDestroy()
        {
            _gamePlayManager.UpdateStatus();
        }

        /// <summary>
        /// Activate ourselves and set our state as being positioned for take-off
        /// </summary>
        public void Activate()
        {
            gameObject.SetActive(true);
            State = RocketState.Positioning;
            GetComponent<AudioSource>().clip = Fuse;
            GetComponent<AudioSource>().Play();
        }

        /// <summary>
        /// Deactivate ourselves
        /// </summary>
        public void Deactivate()
        {
            State = RocketState.Dormant;
            GetComponent<AudioSource>().Stop();
        }

        /// <summary>
        /// Fire the rocket, set a time for destruction then play appropriate 
        /// sound and shake the camera for effect
        /// </summary>
        public void Fire()
        {
            State = RocketState.Firing;
            Destroy(gameObject, _lifeTime);
            GetComponent<AudioSource>().clip = TakeOff;
            GetComponent<AudioSource>().Play();
            Camera.main.GetComponent<ShakeCamera>().Shake();
        }

        /// <summary>
        /// Clamps the rocket to the nearest point on the slider baed on the horizontal position of the mouse
        /// </summary>
        void ClampRocketToSlider()
        {
            Vector3 originScreenPos = Camera.main.WorldToScreenPoint(SliderOrigin.position);
            Vector3 endPointScreenPos = Camera.main.WorldToScreenPoint(SliderEndPoint.position);
            float fraction = (Input.mousePosition.x - originScreenPos.x) / (endPointScreenPos.x - originScreenPos.x);
            Vector3 ClosestPoint3D = Vector3.Lerp(SliderOrigin.position, SliderEndPoint.position, fraction);
            transform.position = ClosestPoint3D;
        }

        /// <summary>
        /// Udpate the vertical position of the missile (during take-off)
        /// </summary>
        void UpdateVerticalPosition()
        {
            transform.Translate(Vector3.up * _speed);
        }

        /// <summary>
        /// Returns true if the rocket is currently active, false otherwise
        /// </summary>
        public bool IsActive
        {
            get { return State == RocketState.Positioning; }
        }

        /// <summary>
        /// Returns true if the rocket has been fired, false otherwise
        /// </summary>
        public bool HasFired
        {
            get { return State == RocketState.Firing; }
        }

        /// <summary>
        /// Returns true if the rocket is currently dormant/inactive, false otherwise
        /// </summary>
        public bool IsInactive
        {
            get { return State == RocketState.Dormant; }
        }
    }
}

#if NO_AND_ERROR
public void TEXT1() 
{
	int textNum = 1;
	if (textNum == 1) 
	{
		textNum = 2;
    }
}
public void TEXT2()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}
public void TEXT3()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT4()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT5()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT6()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT8()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT9()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT10()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT11()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT12()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT13()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT14()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT15()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT16()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT17()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}
#endif